package com.siqi.hr.system.custom.service;

import com.siqi.hr.common.utils.Const;
import com.siqi.hr.system.common.enums.OperationEnum;
import com.siqi.hr.system.custom.dao.intf.SelectBoxDao;
import com.siqi.hr.system.custom.entity.EntityOperation;
import com.siqi.hr.system.custom.entity.EntityRecord;
import com.siqi.hr.system.custom.entity.SelectBox;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典sercice层
 * @author    xiaogq
 * Created by xiaogoingqin on 2016/7/24.
 */
@Service("selectBoxService")
@Transactional(rollbackFor = { Exception.class }, propagation = Propagation.REQUIRED)
public class SelectBoxService {

    @Resource
    private SelectBoxDao selectBoxDao;

    /**
     * 单选、下拉、下拉多选列表页表头信息
     * @author    xiaogq
     * @return
     */
    public Map<String, Object> querySelectBoxListColumns() {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        StringBuffer columns = new StringBuffer();
        columns.append("<th data-field='operation' data-formatter='showColOperator'>操作</th>");
        columns.append("<th data-field='id' data-visible='false'>ID</th>");
        columns.append("<th data-field='name' data-sortable='true'>值</th>");
        columns.append("<th data-field='default_value'>默认值</th>");
        columns.append("<th data-field='vals_comment'>描述</th>");
        columnMap.put("fields", "");
        columnMap.put("columns", columns);
        return columnMap;
    }

    /**
     * 根据字段id查询单选、下拉、下拉多选类型字段所有枚举值信息
     * @author    xiaogq
     * @param fieldId     注册字段id
     */
    public Map<String, Object> queryEntityData(Integer fieldId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //根据字段id查询单选、下拉、下拉多选类型字段所有枚举值信息
        List<Map<String, Object>> entitysData = selectBoxDao.getSelectBoxsData(fieldId);

        for (Map<String, Object> map : entitysData) {
            Object dfvObj = map.get("default_value");
            Object sys_record = map.get("sys_record");
            List<EntityOperation> optionList = new ArrayList<EntityOperation>();
            //所有实体默认都有编辑按钮
            EntityOperation editField = new EntityOperation("编辑","editSelectBoxEnum","FieldRecord/selectBoxEdit.do","glyphicon glyphicon-edit", OperationEnum.showColumn.name(),new EntityRecord(0));
            optionList.add(editField);
            if (sys_record != null && !Boolean.parseBoolean(sys_record.toString())) {
                EntityOperation deleteField = new EntityOperation("删除","delSelectBoxEnum","FieldRecord/fieldRecordEnumValueDelete.do","glyphicon glyphicon-trash",OperationEnum.showColumn.name(),new EntityRecord(0));
                optionList.add(deleteField);
            }
            map.put("operation",optionList);
            if (dfvObj != null && Boolean.parseBoolean(dfvObj.toString())) {
                map.put("default_value", "是");
            }else {
                map.put("default_value", "");
            }
        }
        resultMap.put(Const.TOTAL, 100);
        resultMap.put(Const.ROWS, entitysData);

        return resultMap;
    }

    /**
     * 根据字段id查询单选、下拉、下拉多选类型字段所有枚举值信息
     * @author             xiaogq
     * @param fieldId     注册字段id
     */
    public List<Map<String, Object>> getSelectBoxsData(Integer fieldId) {
        return selectBoxDao.getSelectBoxsData(fieldId);
    }

    /**
     * 添加枚举类型字段时添加枚举值
     * @author    xiaogq
     * @param selectBox
     */
    public int addSelectBox(SelectBox selectBox) {
        return selectBoxDao.addSelectBox(selectBox);
    }

    /**
     * 根据枚举值id获取记录信息
     * @author    xiaogq
     * @param     selectBoxId    枚举值id
     */
    public SelectBox getSelectBox(int selectBoxId) {
        return selectBoxDao.getSelectBox(selectBoxId);
    }

    /**
     * 编辑枚举型字段
     * @author    xiaogq
     * @param selectBox    枚举值对象信息
     */
    public Map<String, Object> updateSelectBox(SelectBox selectBox) {
        Map<String, Object> resultMap = new HashMap<String,Object>();
        //编辑枚举型字段
        int counts = selectBoxDao.updateSelectBox(selectBox);
        resultMap.put("flag",counts > 0);
        return resultMap;
    }

    /**
     * 根据字段id查询所有的枚举字段
     * @author    xiaogq
     * @param     fieldId    字段id
     * @param     ids         枚举值id字符串
     */
    public List<SelectBox> getAllSelectBoxByFieldId(int fieldId,String ids) {
        return selectBoxDao.getAllSelectBoxByFieldId(fieldId,ids);
    }

    /**
     * 保存枚举排序方法
     * @param sortIds     所有排序枚举值id
     * @return            JSONObject
     */
    public JSONObject selectBoxSortUpdate(String sortIds) {
        JSONObject obj = new JSONObject();
        //修改枚举排序方法
        int nums = selectBoxDao.selectBoxSortUpdate(sortIds);
        obj.put("flag",nums > 0);
        return obj;
    }

    /**
     * 根据枚举id删除枚举字段
     * @author    xiaogq
     * @param selectBoxId    枚举值id
     */
    public Map<String, Object> deleteSelectBox(Integer selectBoxId) {
        Map<String, Object> map = new HashMap<String, Object>();
        //根据枚举值id获取枚举对象信息
        SelectBox selectBox = selectBoxDao.getSelectBox(selectBoxId);
        //如果是系统枚举值则不能删除
        if(selectBox.isSysRecord()) {
            map.put("suc", "sys");
        } else {
            try {
                //根据枚举值id删除枚举信息
                selectBoxDao.deleteSelectBox(selectBoxId);
                map.put("suc", "success");
            } catch (RuntimeException e) {
                e.printStackTrace();
                map.put("suc", "error");
            }
        }
        return map;
    }

    /**
     * 通过实体id查询select
     * @author ChenL
     */
    public List<Map<String,Object>> getByEntityId(int entityId){
        return selectBoxDao.getByEntityId(entityId);
    }

}
